package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.TbBrandService;
import com.dongdongshop.pojo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("TbBrand")
public class TbBrandController {


    //注入service
    @DubboReference
    private TbBrandService tbBrandService;

    //查询全部品牌列表(要求分页)
    @ResponseBody
    @RequestMapping("TbBrandshowall")
    public Result TbBrandshowall(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "6") Integer pageSize) {
        try {
            PageResult<TbBrand> tbBrands = tbBrandService.TbBrandshowall(pageNum, pageSize);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(tbBrands);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //查询全部品牌列表
    @ResponseBody
    @RequestMapping("TbBrandshow")
    public Result TbBrandshow() {
        try {
            List<TbBrand> tbBrands = tbBrandService.TbBrandshow();
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(tbBrands);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //添加品牌
    @ResponseBody
    @RequestMapping("addBrand")
    public Result addBrand(TbBrand tbBrand) {
        try {
            tbBrandService.addBrand(tbBrand);
            return Result.response(ResultEnums.ADD_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.ADD_ERROR);
        }
    }

    //回显单个品牌数据
    @ResponseBody
    @RequestMapping("showecho")
    public Result showecho(Long id) {
        try {
            TbBrand tbBrand = tbBrandService.showecho(id);
            return Result.response(ResultEnums.ECHO_SUCCESS).setData(tbBrand);
        } catch (Exception e) {
            return Result.response(ResultEnums.ECHO_ERROR);
        }
    }

    //修改单个数据
    @ResponseBody
    @RequestMapping("updateBrand")
    public Result updateBrand(TbBrand tbBrand) {
        try {
            tbBrandService.updateBrand(tbBrand);
            return Result.response(ResultEnums.UPDATE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.UPDATE_ERROR);
        }
    }

    //删除单个数据
    @ResponseBody
    @RequestMapping("deleteBrand")
    public Result deleteBrand(Long id) {
        try {
            tbBrandService.deleteBrand(id);
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }

    //删除多个数据
    @ResponseBody
    @RequestMapping("deleteallBrand")
    public Result deleteallBrand(@RequestParam(value = "ids[]") List<Long> ids) {
        try {
            tbBrandService.deleteallBrand(ids);
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }
}
